package com.slwish.xty.ui.activity;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.slwish.common.base.PresenterToolbarActivity;
import com.slwish.common.config.Contants;
import com.slwish.common.utils.IntentUtils;
import com.slwish.xty.R;
import com.slwish.xty.adapter.CreditTourismListAdapter;
import com.slwish.xty.entity.CreditTourismInfoModel;
import com.slwish.xty.mvp.contract.CreditTourismContract;
import com.slwish.xty.mvp.presenter.CreditTourismPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 商户列表
 */
public class CreditTourismListActivity extends PresenterToolbarActivity<CreditTourismContract.Presenter>
        implements CreditTourismContract.View, XRecyclerView.LoadingListener {

    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    CreditTourismListAdapter mAdapter;
    List<CreditTourismInfoModel> mLists = new ArrayList<>();//列表数据集

    @Override
    protected int getLayoutId() {
        return R.layout.act_credit_tourism_list;
    }


    @Override
    protected void initWidget() {
        super.initWidget();
        setTitle(R.string.credit_tourism_title);
        xrecyclerview.setLayoutManager();
        mAdapter = new CreditTourismListAdapter(mLists);
        xrecyclerview.setLoadingListener(this);
        xrecyclerview.setAdapter(mAdapter);
        xrecyclerview.refresh();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putInt(Contants.ID,mAdapter.getItem(position-1).getMerId());
                bundle.putSerializable(Contants.MODEL,mAdapter.getItem(position-1));
                IntentUtils.startActivity(mContext, CreditTourismInfoActivity.class,bundle);
            }
        });
    }

    @Override
    public void searchMerchantSuccess(List<CreditTourismInfoModel> model) {

        if (model != null && model.size() > 0) {
            if (mPagerCofig.getCurrentPage() == 1) {
                mLists.clear();
                xrecyclerview.refreshComplete();
            }
            mLists.addAll(model);
            xrecyclerview.setNoMore(false);
            if (mPagerCofig.getCurrentPage() != 1)
                mAdapter.notifyItemRangeChanged(mLists.size() - model.size(), mLists.size() - 1);

        } else {
            if (mPagerCofig.getCurrentPage() == 1) {
                //显示无数据界面
                View view = View.inflate(mContext, R.layout.layout_empty, null);
                mAdapter.setEmptyView(view);
                xrecyclerview.refreshComplete();
            }
            xrecyclerview.setNoMore(true);
        }
    }

    /**
     * 失败
     */
    @Override
    public void searchMerchantFailed() {
        if (mLists == null || mLists.size() < 1) {
            View view = View.inflate(mContext, R.layout.layout_timeout, null);
            mAdapter.setEmptyView(view);
            xrecyclerview.refreshComplete();
            xrecyclerview.setNoMore(true);
            view.findViewById(R.id.tv_reset_loading).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xrecyclerview.refresh();
                }
            });
        }
    }

    @Override
    protected CreditTourismContract.Presenter initPresenter() {
        return new CreditTourismPresenter(this);
    }

    @Override
    public void onRefresh() {
        mPagerCofig.setCurrentPage(1);
        mPresenter.searchMerchant("119.697805", "29.70", mPagerCofig.getCurrentPage());
    }

    @Override
    public void onLoadMore() {
        //处理是否要加载分页数据
        if (mAdapter.getItemCount() / 10 == mPagerCofig.getCurrentPage()) {
            mPagerCofig.setCurrentPage(1 + mPagerCofig.getCurrentPage());
            mPresenter.searchMerchant("119.697805", "29.70", mPagerCofig.getCurrentPage());//获取首页列表数据
        } else {
            xrecyclerview.setNoMore(true);
        }

    }
}
